/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.comboBox.cellRenderer;

import java.awt.Component;
import java.awt.Font;
import java.awt.Image;
import java.lang.reflect.Method;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import org.jxUtils.ReflectionUtil;
import org.jxUtils.TypeUtil;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.comboBox.cellRenderer
 * @date 05/02/2014 - 22:37:50
 */
public class LabelListRenderer extends JLabel implements ListCellRenderer<Object> {
	
	private static final long serialVersionUID = 3877517261543133865L;
	
	private static final JLabel LABEL = new JLabel();
	
	/**
     * 
     */
	public LabelListRenderer() {
	
		setOpaque(true);
		
		setHorizontalAlignment(LEADING);
		setVerticalAlignment(CENTER);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
	 */
	@Override
	public Component getListCellRendererComponent(final JList<?> list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
	
		//
		if(isSelected) {
			
			setBackground(list.getSelectionBackground());
			setForeground(list.getSelectionForeground());
			
		}else {
			
			setBackground(list.getBackground());
			setForeground(list.getForeground());
		}
		
		//
		setText(null);
		setFont(LABEL.getFont());
		setIcon(null);
		setToolTipText(null);
		
		//
		if(value != null) {
			
			// Se for um tipo de Imagem
			if(TypeUtil.isImageType(value)) {
				
				ImageIcon imageIcon = null;
				
				//
				if(TypeUtil.isImage(value)) {
					
					imageIcon = new ImageIcon(((Image)value).getScaledInstance(30, 25, Image.SCALE_DEFAULT));
					
				}else if(TypeUtil.isImageIcon(value)) {
					
					imageIcon = new ImageIcon(((ImageIcon)value).getImage().getScaledInstance(30, 25, Image.SCALE_DEFAULT));
					
					imageIcon.setDescription(((ImageIcon)value).getDescription());
					imageIcon.setImageObserver(((ImageIcon)value).getImageObserver());
				}
				
				//
				setText(imageIcon.getDescription());
				setFont(LABEL.getFont());
				setIcon(imageIcon);
				setToolTipText(imageIcon.getDescription());
				
			}else if(value instanceof Font) {
				
				final Font font = (Font)value;
				
				setText(font.getName());
				setFont(font);
				
			}else {
				
				setText(value.toString());
				
				Method method = ReflectionUtil.getFirstMethodGET(value, ImageIcon.class);
				
				//
				if(method != null) {
					
					try {
						// 
						setIcon((ImageIcon)method.invoke(value));
						
					}catch(Throwable throwable) {
						
						throwable.printStackTrace();
					}
				}
			}
		}
		
		return this;
	}
}
